package cn.com.dhc.danlu.widget.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.math.BigDecimal;
import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.CalculatePriceUtil;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.widget.holder.EnvelopHolder;
import cn.com.hd.mall.web.webservices.entity.response.cpx.GetCouponListResponse.CouponListModel;

/**
 * @author wuhaiyang 可用红包 和不可用红包的适配器
 */
public class EnvelopeableAdapter extends MyBaseAdapter {
    private int mType;

    /**
     * 维护当前选中了那些优惠券
     */
    public EnvelopeableAdapter (Context context, int type) {
        super (context);
        this.mType = type;
    }

    public void refreshType (int type) {
        this.mType = type;
        notifyDataSetChanged ();
    }

    public void showHistory (List<CouponListModel> datas) {
        for (CouponListModel model : datas) {
            for (Object obj : mDatas) {
                CouponListModel originData = (CouponListModel) obj;
                if (originData.equals (model)) {
                    originData.setChecked (true);
                    break;
                }
            }
        }

        notifyDataSetChanged ();
    }

    @SuppressLint ("SetTextI18n")
    @Override
    protected View createView (int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            convertView = mInflater.inflate (R.layout.item_envelopable, parent, false);
            holder = new ViewHolder (convertView);
            convertView.setTag (holder);
        } else {
            holder = (ViewHolder) convertView.getTag ();
        }
        CouponListModel data = (CouponListModel) getItem (position);

        if (mType == 0) {
            holder.iv_item.setVisibility (View.VISIBLE);
            if (data.isChecked ()) {
                holder.iv_item.setImageResource (R.drawable.icon_envelop_checknormal);
            } else {
                holder.iv_item.setImageResource (R.drawable.icon_envelop_checkpress);
            }
            holder.iv_useenvelop_bg.setImageResource (R.drawable.icon_canuse_envelop_bg);
            holder.tv_money.setTextColor (mInflater.getContext ().getResources ().getColor (R.color.color_ca382b));
            holder.tv_dayleft.setTextColor (mInflater.getContext ().getResources ().getColor (R.color.color_ca382b));
        } else {
            holder.iv_item.setVisibility (View.GONE);
            holder.iv_useenvelop_bg.setImageResource (R.drawable.icon_disableuse_envelop_bg);
            holder.tv_money.setTextColor (mInflater.getContext ().getResources ().getColor (R.color.btn_txt_color));
            holder.tv_dayleft.setTextColor (mInflater.getContext ().getResources ().getColor (R.color.btn_txt_color));
        }
        holder.tv_money.setText ("￥" + CalculatePriceUtil.converseAmount (CommonUtils.fenToYuan (new BigDecimal (data.getCouponAmt ())).toPlainString ()));
        if (TextUtils.isEmpty (data.getTimeLeft ())) {
            holder.tv_dayleft.setText ("还有0天过期");
        } else {
            holder.tv_dayleft.setText ("还有" + data.getTimeLeft () + "天过期");
        }
        holder.tv_envelop_title.setText (data.getCouponName ());
        holder.tv_envelop_num.setText ("红包编号：" + data.getCouponCode ());
        String limitStr = "品类限制：";
        if (!TextUtils.isEmpty (data.getKindsLimit ())) {
            limitStr += data.getKindsLimit ();
        } else {
            limitStr += "全场通用";
        }
        holder.tv_limit.setText (limitStr);
        holder.tv_end_time.setText ("有效期至：" + data.getExpireDay ());

        return convertView;
    }

    private static class ViewHolder extends EnvelopHolder {
        ImageView iv_item;
        ImageView iv_useenvelop_bg;//优惠券背景
        TextView tv_money;//
        TextView tv_dayleft;
        TextView tv_envelop_title;
        TextView tv_envelop_num;
        TextView tv_limit;
        TextView tv_end_time;

        public ViewHolder (View view) {
            super (view);
            this.iv_item = (ImageView) view.findViewById (R.id.iv_item);
            this.iv_useenvelop_bg = (ImageView) view.findViewById (R.id.iv_useenvelop_bg);
            this.tv_money = (TextView) view.findViewById (R.id.tv_money);
            this.tv_dayleft = (TextView) view.findViewById (R.id.tv_dayleft);
            this.tv_envelop_title = (TextView) view.findViewById (R.id.tv_envelop_title);
            this.tv_envelop_num = (TextView) view.findViewById (R.id.tv_envelop_num);
            this.tv_limit = (TextView) view.findViewById (R.id.tv_limit);
            this.tv_end_time = (TextView) view.findViewById (R.id.tv_end_time);
        }
    }
}